﻿Ext.define("PSI.BaseCode.Admin.Metadata.Column.MetadataColumnEditForm", {

    createUI: function (medi) {
        var me = this;
        var addmetadatacolumnwindow = Ext.create("Ext.window.Window", {
            width: 400,
            height: 290,
            layout: "fit",
            title: "新增码表字段",
            modal: true,
            onEsc: Ext.emptyFn,
            items: [
               {
                   id: "editForm",
                   xtype: "form",
                   layout: "form",
                   height: "100%",
                   bodyPadding: 5,
                //   border: false,
                   bodyStyle: 'border-width:0 0 1px 0',
                   defaultType: 'textfield',
                   fieldDefaults: {
                       labelWidth: 100,
                       labelAlign: "right",
                       labelSeparator: "",
                       msgTarget: 'side'
                   },
                   items: [
                        {
                            id:"metadata",
                            xtype: "hidden",
                            name: "basecodeId",
                            value: medi
                        },
                        {
                            id: "ColumnName",
                            fieldLabel: "列字段名",
                            name: "ColumnName",
                            allowBlank: false,
                            blankText: "没有输入名称",
                            beforeLabelTextTpl: PSI.Const.REQUIRED
                        },
                        {
                            id: "columntitle",
                            fieldLabel: "列字段中文名",
                            name: "columnTitle"
                        },
                        me.columnTypeDropDown(),
                        {
                            xtype: 'numberfield',
                            id: "columnlength",
                            fieldLabel: "列长度",
                            name: "columnLength",
                            minValue: 0
                        },
                        {
                            id: "defultvalue",
                            fieldLabel: "列默认值",
                            name: "defultValue"
                        },
                        {
                            xtype: 'numberfield',
                            id: "showorder",
                            fieldLabel: "列的显示次序",
                            name: "showOrder",
                            minValue: 0,
                            maxValue: 100
                        },
                        {
                            id: "isvisible",
                            fieldLabel: "列对用户是否可见",
                            name: "isVisible",
                            xtype: 'checkboxgroup',
                        // Arrange checkboxes into two columns, distributed vertically
                          //  columns: 2,
                            vertical: true,
                            items: [
                                { boxLabel: '可见', name: 'isvisible', inputValue: 1, checked: true },
                                { boxLabel: '不可见', name: 'isvisible', inputValue: 0 },
                            ]

                        },
                        {
                            id: "displayfmt",
                            fieldLabel: "显示格式",
                            name: "displayFmt"
                        },

                   ],
                   buttons: [
                    {
                        text: "确定",
                        formBind: true,
                        iconCls: "psi-button-ok",
                        handler: function () {me.windowSubmit()  },
                        scope: this
                    },
                    {
                        text: "取消", handler: function () {
                            PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                me.windows.close();
                            });
                        }, scope: this
                    }
                   ]
               }
            ]
        });

        addmetadatacolumnwindow.show();
 
        this.windows = addmetadatacolumnwindow;
        
    },

    show: function (medi) {

       return this.createUI(medi);

    },

    columnTypeDropDown: function () {        

        // Create the combo box, attached to the states data store
        var ddl= Ext.create('Ext.form.ComboBox', {
            fieldLabel: '数据类型',
            store: Ext.create('Ext.data.Store', {
                fields: ['abbr', 'name'],
                data: [
                    { "abbr": "datetime", "name": "datetime" },
                    { "abbr": "int", "name": "int" },
                    { "abbr": "varchar", "name": "varchar" },
                    { "abbr": "float", "name": "float" }
                ]
            }),
            // queryMode: 'local'
            name: "ColumnType",
            displayField: 'name',
            valueField: 'abbr',
            allowBlank: false,
            blankText: "没有输入名称",
            beforeLabelTextTpl: PSI.Const.REQUIRED
            // renderTo: Ext.getBody()
        })

        return ddl;
    },

    windowSubmit:function(){
        var me = this;
        var metadataid = Ext.getCmp("metadata").getValue();
        var f = Ext.getCmp("editForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/BaseCodeAdminMetadataColumn/AddColumn",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                var cc = action.result.success;
                if (cc) {
                    PSI.MsgBox.showInfo("数据保存成功", function () {
                        me.windows.close();

                        var treestore = Ext.getCmp("caregorytree").getStore();

                        var note = treestore.getNodeById(metadataid);

                        Ext.getCmp("caregorytree").getSelectionModel().clearSelections();

                        Ext.getCmp("caregorytree").getSelectionModel().select(note);
                        

                    });
                }
                else {
                    Ext.Msg.alert('Failure', action.result.msg);
                }
            },
            failure: function (form, action) {
                el.unmask();
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('Failure', 'ajax链接失败!');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', action.result.msg);
                }
            }
        });


    }

})